/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SubsitemaAgenda;

import com.toedter.calendar.JCalendar;
import java.awt.Dimension;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import service.Cita;
import service.Paciente;

/**
 *
 * @author Mario
 */
public class DialogoBuscarCitas extends java.awt.Dialog {

    /**
     * Creates new form DialogoBuscarCitas
     */
    private List<service.Cita> citas;
    private JCalendar calendar;
    service.Paciente paciente;
    
    public DialogoBuscarCitas(List<service.Cita> list, JCalendar calendar) {
        super(new JFrame(), true);
        paciente = (Paciente) cargarPaciente(list.get(0).getRutPaciente());
        this.setTitle("Citas de "+paciente.getNombres()+" "+paciente.getApellidoPaterno()+" "+paciente.getApellidoMaterno());   
        this.calendar= calendar;
        this.citas = list;
        initComponents();
        this.setLocationRelativeTo(null);
        setIconImage(new ImageIcon(getClass().getResource("/GUI/Img/logo2.png")).getImage());
        SimpleDateFormat formatoDeFecha = new SimpleDateFormat("dd/MM/yyyy");
        this.jLabel1.setText(formatoDeFecha.format(calendar.getDate()));
        
        this.botonPosteriorActionPerformed(null);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        botonAnterior = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        botonPosterior = new javax.swing.JToggleButton();

        setMinimumSize(new java.awt.Dimension(800, 400));
        setPreferredSize(new java.awt.Dimension(800, 400));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jScrollPane1.setPreferredSize(new java.awt.Dimension(452, 60));

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTable1.setToolTipText("");
        jTable1.setMinimumSize(new java.awt.Dimension(452, 60));
        jTable1.setName(""); // NOI18N
        jTable1.setPreferredSize(new java.awt.Dimension(452, 60));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                seleccionaCita(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        botonAnterior.setBackground(new java.awt.Color(255, 255, 255));
        botonAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/flechaAnterior.png"))); // NOI18N
        botonAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAnteriorActionPerformed(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(255, 0, 0));
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("fecha");

        botonPosterior.setBackground(new java.awt.Color(255, 255, 255));
        botonPosterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/flechaPosterior.png"))); // NOI18N
        botonPosterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonPosteriorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(314, 314, 314)
                .addComponent(botonAnterior)
                .addGap(5, 5, 5)
                .addComponent(jLabel1)
                .addGap(5, 5, 5)
                .addComponent(botonPosterior))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(botonAnterior))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addComponent(jLabel1))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(botonPosterior))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Closes the dialog
     */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void botonAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAnteriorActionPerformed
        this.botonAnterior.setSelected(true);
        this.botonPosterior.setSelected(false);
        this.botonAnterior.setEnabled(false);
        this.botonPosterior.setEnabled(true);
        //anterior
        ArrayList<service.Cita> list = new ArrayList<service.Cita>();                                        
        for(int i = 0 ; i<this.citas.size(); i++){  
            System.out.println();
            if(this.calendar.getDate().after(citas.get(i).getFecha().toGregorianCalendar().getTime())){
                list.add(citas.get(i));                
            }            
        }
        agregarCitas(list);
    }//GEN-LAST:event_botonAnteriorActionPerformed

    private void botonPosteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonPosteriorActionPerformed
        this.botonPosterior.setSelected(true);
        this.botonAnterior.setSelected(false);
        this.botonPosterior.setEnabled(false);
        this.botonAnterior.setEnabled(true);
        ArrayList<service.Cita> list = new ArrayList<service.Cita>();        
        for(int i = 0 ; i<this.citas.size(); i++){                       
            if(this.calendar.getDate().before(citas.get(i).getFecha().toGregorianCalendar().getTime())){
                list.add(citas.get(i));                
            }            
        }
        agregarCitas(list);
        
    }//GEN-LAST:event_botonPosteriorActionPerformed

    private void seleccionaCita(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_seleccionaCita
        if (evt.getClickCount() == 2) { 
            service.Cita cita = (service.Cita) buscarCitaPorId((Long)this.jTable1.getValueAt(this.jTable1.getSelectedRow(), 0));                                  
                this.calendar.setDate(cita.getFecha().toGregorianCalendar().getTime());
                this.dispose();
                     
        }
    }//GEN-LAST:event_seleccionaCita
    private void agregarCitas(List<service.Cita> list){
        String[] columNames ={"id","Rut paciente", "Nombre paciente", "Fecha", "Horario", "Estado", "Numero sesión", "Observacion"};
                Object[][] data = new Object[list.size()][columNames.length];
                for(int i = 0 ; i < list.size(); i++){     
                        data[i][0] = list.get(i).getId();
                        data[i][1] = list.get(i).getRutPaciente();
                        data[i][2] = paciente.getNombres()+" "+paciente.getApellidoPaterno()+" "+paciente.getApellidoMaterno();
                        data[i][3] = new SimpleDateFormat("dd/MM/yyyy").format(list.get(i).getFecha().toGregorianCalendar().getTime());
                        data[i][4] = list.get(i).getDesdeHora()+":"+list.get(i).getDesdeMinuto();
                        data[i][5] = list.get(i).getEstado();
                        data[i][6] = list.get(i).getNumeroSesion();
                        data[i][7] = list.get(i).getObservacion();                                
                }
                this.jTable1.setModel(new DefaultTableModel(data, columNames){public boolean isCellEditable(int row, int column){return false;}});                                                 
    }
    private int mesANumero(String b){
        int mes = 0;
        if(b.equals("enero"))
            mes = 1;
        if(b.equals("febrero"))
            mes = 2;
        if(b.equals("marzo"))
            mes = 3;
        if(b.equals("abril"))
            mes = 4;
        if(b.equals("mayo"))
            mes = 5;
        if(b.equals("junio"))
            mes = 6;
        if(b.equals("julio"))
            mes = 7;
        if(b.equals("agost"))
            mes = 8;
        if(b.equals("septiembre"))
            mes = 9;
        if(b.equals("octubre"))
            mes = 10;
        if(b.equals("noviembre"))
            mes = 11;
        if(b.equals("diciembre"))
            mes = 12;
        return mes;
    }
   
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton botonAnterior;
    private javax.swing.JToggleButton botonPosterior;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private static Object buscarCitaPorId(java.lang.Long id) {
        service.AgendaWS_Service service = new service.AgendaWS_Service();
        service.AgendaWS port = service.getAgendaWSPort();
        return port.buscarCitaPorId(id);
    }

    private static Object cargarPaciente(java.lang.String rut) {
        service.PacienteWS_Service service = new service.PacienteWS_Service();
        service.PacienteWS port = service.getPacienteWSPort();
        return port.cargarPaciente(rut);
    }
}
